package timing.ukulele.portal.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import timing.ukulele.common.data.ResponseData;
import timing.ukulele.data.portal.view.SysApplicationVO;
import timing.ukulele.facade.portal.mvc.SysApplicationControllerFacade;
import timing.ukulele.portal.convertor.ApplicationConverter;
import timing.ukulele.portal.persistent.SysApplication;
import timing.ukulele.portal.service.SysApplicationService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
public class SysApplicationController implements SysApplicationControllerFacade {
    private final SysApplicationService service;

    public SysApplicationController(SysApplicationService service) {
        this.service = service;
    }

    @Override
    public ResponseData<SysApplicationVO> application(Long id) {
        SysApplication po = this.service.getById(id);
        if (po != null) {
            SysApplicationVO vo = new SysApplicationVO();
            BeanUtils.copyProperties(po, vo);
            return ResponseData.success(vo);
        }
        return ResponseData.error("不存在");
    }

    @Override
    public ResponseData<List<SysApplicationVO>> allApplication(Boolean deleted) {
        if (deleted == null) deleted = false;
        List<SysApplication> list = this.service.lambdaQuery().eq(SysApplication::getDeleted, deleted).list();
        if (CollectionUtils.isEmpty(list)) {
            return ResponseData.success();
        }
        List<SysApplicationVO> vos = new ArrayList<>(list.size());
        for (SysApplication po : list) {
            SysApplicationVO vo = new SysApplicationVO();
            BeanUtils.copyProperties(po, vo);
            vos.add(vo);
        }
        return ResponseData.success(vos);
    }

    @Override
    public ResponseData<SysApplicationVO> applicationByCode(String code) {
        SysApplication one = this.service.lambdaQuery().eq(SysApplication::getCode, code).one();
        if (one != null) {
            SysApplicationVO vo = new SysApplicationVO();
            BeanUtils.copyProperties(one, vo);
            return ResponseData.success(vo);
        }
        return ResponseData.error("不存在");
    }

    @Override
    public ResponseData<Boolean> add(String currentUser, SysApplicationVO vo) {
        SysApplication po = new SysApplication();
        BeanUtils.copyProperties(vo, po);
        po.setCreateBy(currentUser);
        po.setUpdateBy(currentUser);
        boolean save = this.service.save(po);
        return save ? ResponseData.success(Boolean.TRUE) : ResponseData.error("保存失败");
    }

    @Override
    public ResponseData<Boolean> edit(String currentUser, SysApplicationVO vo) {
        if (vo.getId() == null) {
            return ResponseData.paramError();
        }
        SysApplication po = new SysApplication();
        BeanUtils.copyProperties(vo, po);
        po.setUpdateBy(currentUser);
        boolean save = this.service.saveOrUpdate(po);
        return save ? ResponseData.success(Boolean.TRUE) : ResponseData.error("保存失败");
    }

    @Override
    public ResponseData<Boolean> delete(String currentUser, Long id) {
        if (id == null) {
            return ResponseData.paramError();
        }
        this.service.lambdaUpdate().eq(SysApplication::getId, id)
                .set(SysApplication::getDeleted, Boolean.TRUE)
                .set(SysApplication::getUpdateBy, currentUser)
                .set(SysApplication::getUpdateTime, LocalDateTime.now()).update();
        return ResponseData.success();
    }


    @GetMapping("/page/{current}/{size}")
    public ResponseData<Page<SysApplicationVO>> getPage(@PathVariable(name = "current") int current,
                                                        @PathVariable(value = "size") int size,
                                                        @RequestParam(value = "code", required = false) String code,
                                                        @RequestParam(value = "name", required = false) String name,
                                                        @RequestParam(value = "deleted", required = false) Boolean deleted) {
        if (size == 0) {
            size = 10;
        }
        if (current == 0) {
            current = 1;
        }
        Page<SysApplicationVO> page = (Page<SysApplicationVO>) this.service.getPage(code, name, deleted, current, size);
        return ResponseData.success(page);
    }

    @Override
    public ResponseData<List<SysApplicationVO>> getApplicationList(@RequestParam(value = "code", required = false) String code,
                                                        @RequestParam(value = "name", required = false) String name,
                                                        @RequestParam(value = "deleted", required = false) Boolean deleted) {
        List<SysApplication> list = this.service.lambdaQuery()
                .like(ObjectUtil.isNotEmpty(code), SysApplication::getCode, code)
                .like(ObjectUtil.isNotEmpty(name), SysApplication::getName, name)
                .eq(ObjectUtil.isNotEmpty(deleted), SysApplication::getDeleted, deleted)
                .list();
        List<SysApplicationVO> sysApplicationVOS = ApplicationConverter.INSTANCE.p2vList(list);
        return ResponseData.success(sysApplicationVOS);
    }
}
